//
//  ImagePickerAction.m
//  CarService
//
//  Created by fengyn on 15/2/9.
//  Copyright (c) 2015年 lexingsoft. All rights reserved.
//

#import "ImagePickerAction.h"
#import <AssetsLibrary/AssetsLibrary.h>
#import <MobileCoreServices/MobileCoreServices.h>
#import "PromptAction.h"
#import <UIActionSheet+Blocks.h>
#import "TZImagePickerController.h"
//#import "XHPhotoOptionViewController.h"

#define WEAKSELF_SC __weak __typeof(&*self)weakSelf_SC = self;

@interface ImagePickerAction ()

@property (nonatomic, weak) UIViewController *showViewController;
@property (nonatomic) int mutableSelectCount;

@end

@implementation ImagePickerAction

- (instancetype) initWithShowViewController:(UIViewController *)viewController
{
    self = [super init];
    if (self) {
        self.showViewController = viewController;
        self.mutableSelect = NO;
    }
    return self;
}

- (void) showImagePickerWithBlock:(PickerImageBlock) imagePicker;
{
    [self showImagePickerWithNeedEdit:YES Block:imagePicker];
}

- (void) showImagePickerWithNeedEdit:(BOOL ) needEdit Block:(PickerImageBlock) imagePicker
{
    [self showImagePickerWithNeedEdit:needEdit MaxCount:0 Block:imagePicker];
}

- (void) showImagePickerWithMaxCount:(int) maxCount Block:(PickerImageBlock) imagePicker
{
    [self showImagePickerWithNeedEdit:NO MaxCount:maxCount Block:imagePicker];
}

- (void) showImagePickerWithNeedEdit:(BOOL) needEdit MaxCount:(int) maxCount Block:(PickerImageBlock) imagePicker{
    
    WEAKSELF_SC
    // 选择图片
    RIButtonItem *pickerImageItem = [RIButtonItem itemWithLabel:NSLocalizedString(@"ButtonTitle_SelectPhotos", nil) action:^{
        if (weakSelf_SC.mutableSelect) {
            [weakSelf_SC pushMutableImageSelect];
        }else {
            [weakSelf_SC showVCImagePickWithSourceType:UIImagePickerControllerSourceTypePhotoLibrary canEdit:needEdit];
        }
    }];
    
    // 拍照
    RIButtonItem *takeImageItem = [RIButtonItem itemWithLabel:NSLocalizedString(@"ButtonTitle_Photograph", nil) action:^{
        [weakSelf_SC showVCImagePickWithSourceType:UIImagePickerControllerSourceTypeCamera canEdit:needEdit];
    }];
    
    // 取消
    RIButtonItem *cancelImageItem = [RIButtonItem itemWithLabel:NSLocalizedString(@"ButtonTitle_Cancel", nil) action:^{
        
    }];
    
    self.imageBlock = imagePicker;
    self.maxSelectCount = maxCount;
    
    UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:nil
                                                     cancelButtonItem:cancelImageItem
                                                destructiveButtonItem:nil
                                                     otherButtonItems:takeImageItem, pickerImageItem, nil];
    [actionSheet showInView:self.showViewController.view];

    
//    UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:nil
//                                                     cancelButtonItem:cancelImageItem
//                                                destructiveButtonItem:nil
//                                                     otherButtonItems:takeImageItem, pickerImageItem, nil];
//    [actionSheet showInView:self.showViewController.view];
//    XHPhotoOptionViewController *viewController=[[XHPhotoOptionViewController alloc] init];
//    [viewController setButtonActionBlock:^(XHPhotoOptionViewType type) {
//        switch (type) {
//            case XHPhotoOptionViewTypePhoto:
//                if (weakSelf_SC.mutableSelect) {
//                    [weakSelf_SC pushMutableImageSelect];
//                }else {
//                    [weakSelf_SC showVCImagePickWithSourceType:UIImagePickerControllerSourceTypePhotoLibrary canEdit:needEdit];
//                }
//                break;
//            case XHPhotoOptionViewTypeCamera:
//                [weakSelf_SC showVCImagePickWithSourceType:UIImagePickerControllerSourceTypeCamera canEdit:needEdit];
//                break;
//            case XHPhotoOptionViewTypeCancel:
//                break;
//        }
//    }];
//    viewController.modalPresentationStyle=UIModalPresentationCustom;
//    [self.showViewController presentViewController:viewController animated:NO completion:nil];
}

- (void) ownShowImageHeaderPickWithPhotoLibraryBlock:(PickerImageBlock)imagePicker
{
    self.imageBlock = imagePicker;
    
    [self showVCImagePickWithSourceType:UIImagePickerControllerSourceTypePhotoLibrary canEdit:YES];
}
- (void) ownShowImageHeaderPickWithCameraBlock:(PickerImageBlock)imagePicker
{
    self.imageBlock = imagePicker;
    
    [self showVCImagePickWithSourceType:UIImagePickerControllerSourceTypeCamera canEdit:YES];
}

- (void) showVCImagePickerWithPhotoLibraryBlock:(PickerImageBlock)imagePicker
{
    self.imageBlock = imagePicker;

    [self showVCImagePickWithSourceType:UIImagePickerControllerSourceTypePhotoLibrary canEdit:NO];
}

- (void) showVCImagePickerWithCameraBlock:(PickerImageBlock)imagePicker
{
    self.imageBlock = imagePicker;

    [self showVCImagePickWithSourceType:UIImagePickerControllerSourceTypeCamera canEdit:NO];
}

- (void) showVCImagePickWithSourceType:(UIImagePickerControllerSourceType) SourceType canEdit:(BOOL) canEdit
{
    
    if ([UIImagePickerController isSourceTypeAvailable:SourceType]) {
        UIImagePickerController *imagePickerCintroller = [[UIImagePickerController alloc] init];
        imagePickerCintroller.sourceType = SourceType;
        if (SourceType == UIImagePickerControllerSourceTypeCamera) {
            imagePickerCintroller.mediaTypes = [[NSArray alloc] initWithObjects: (NSString *) kUTTypeImage, nil];
        }
        if (canEdit) {
            imagePickerCintroller.allowsEditing = YES;
        }
        
        imagePickerCintroller.delegate = self;
        
        [self.showViewController presentViewController:imagePickerCintroller animated:YES completion:^{
            
        }];
    }else {
        
        if (SourceType != UIImagePickerControllerSourceTypeCamera) {
            [PromptAction showErrorPrompt:NSLocalizedString(@"Prompt_AccessCamera", nil)];
        }else{
            [PromptAction showErrorPrompt:NSLocalizedString(@"Prompt_AccessPhotoLibrary", nil)];
        }
    }
}

#pragma mark - UIImagePickerControllerDelegate

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    DLog(@"%@", info);
    
    UIImage *selectImage = nil;
    if (info[UIImagePickerControllerEditedImage]) {
        selectImage = info[UIImagePickerControllerEditedImage];
    }else if (info[UIImagePickerControllerOriginalImage]){
        selectImage = info[UIImagePickerControllerOriginalImage];
    }
    
    if (!selectImage)
        return;
    
    WEAKSELF_SC
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        UIImage *resizeImage  = nil;
        if (picker.allowsEditing == YES) {
            resizeImage = [PublicMethod resizeImageWithImage:selectImage SpecialSize:YES];
        }else {
            resizeImage = [PublicMethod resizeImageWithImage:selectImage SpecialSize:NO];
        }
       
        dispatch_async(dispatch_get_main_queue(), ^{
            weakSelf_SC.imageBlock(@[resizeImage]);
        });
    });
    
    [picker dismissViewControllerAnimated:YES completion:^{
        
    }];
}

- (void) pushMutableImageSelect
{
    WEAKSELF_SC
    TZImagePickerController *imagePickerVc = [[TZImagePickerController alloc] initWithMaxImagesCount:self.maxSelectCount delegate:self];
    imagePickerVc.navigationBar.barTintColor=[ColorManager colorWithNavigationBarBackground];
    imagePickerVc.allowPickingOriginalPhoto=NO;
    imagePickerVc.allowPickingVideo=NO;
    // You can get the photos by block, the same as by delegate.
    // 你可以通过block或者代理，来得到用户选择的照片.
    [imagePickerVc setDidFinishPickingPhotosHandle:^(NSArray<UIImage *> *photos, NSArray *assets) {
        dispatch_async(dispatch_get_main_queue(), ^{
            weakSelf_SC.imageBlock(photos);
        });
    }];
    [self.showViewController presentViewController:imagePickerVc animated:YES completion:nil];
    /*
    MutableSelectImage *mutableSelectImageVc = [[MutableSelectImage alloc] initWithNibName:@"MutableSelectImage" bundle:nil];
    mutableSelectImageVc.maxSelectCount = self.maxSelectCount;
    mutableSelectImageVc.mutableSelectImageBlock = ^(NSArray *imageArray){
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
            
            NSMutableArray *tempImageArray = [[NSMutableArray alloc] initWithCapacity:0];
            for (UIImage *selectImage in imageArray) {
                UIImage *resizeImage = [PublicMethod resizeImageWithImage:selectImage SpecialSize:NO];
                [tempImageArray addObject:resizeImage];
            }
            dispatch_async(dispatch_get_main_queue(), ^{
                weakSelf_SC.imageBlock(tempImageArray);
            });
        });
    };
    
    [self.showViewController.navigationController pushViewController:mutableSelectImageVc animated:YES];
    */
}

- (void)navigationController:(UINavigationController *)navigationController
      willShowViewController:(UIViewController *)viewController
                    animated:(BOOL)animated {
    
    if ([navigationController isKindOfClass:[UIImagePickerController class]] &&
        ((UIImagePickerController *)navigationController).sourceType == UIImagePickerControllerSourceTypePhotoLibrary) {
        [[UIApplication sharedApplication] setStatusBarHidden:NO];
        [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent animated:NO];
    }
}

@end